﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using l1cs.server.datatables;
using l1cs.server.templates;
using l1cs.server.model;

namespace l1cs.server
{
    public class HouseTaxTimeController
    {
        private static Logger _log = Logger.getLogger( typeof( HouseTaxTimeController ).FullName );

        private static HouseTaxTimeController _instance;

        public static HouseTaxTimeController getInstance()
        {
            if ( _instance == null )
            {
                _instance = new HouseTaxTimeController();
            }
            return _instance;
        }

        public void threadSub( Object state )
        {
            while ( true )
            {
                checkTaxDeadline();
                Thread.Sleep( 600000 );
            }
        }

        private void checkTaxDeadline()
        {
            foreach ( L1House house in HouseTable.getInstance().getHouseTableList() )
            {
                if ( !house.isOnSale() && house.getTaxDeadline() < DateTime.Now )
                {
                    // 競売中のアジトおよび競売時間内
                    sellHouse( house );
                }
            }
        }

        private void sellHouse( L1House house )
        {
            AuctionBoardTable boardTable = new AuctionBoardTable();
            L1AuctionBoard board = new L1AuctionBoard();
            if ( board != null )
            {
                // 競売掲示板に新規書き込み
                int houseId = house.getHouseId();
                board.setHouseId( houseId );
                board.setHouseName( house.getHouseName() );
                board.setHouseArea( house.getHouseArea() );
                //TimeZone tz = TimeZone.getTimeZone(Config.TIME_ZONE);
                DateTime dateTime = DateTime.Today.AddDays( 5 );// 5日後で分、秒は切り捨て
                board.setDeadline( dateTime );
                board.setPrice( 100000 );
                board.setLocation( house.getLocation() );
                board.setOldOwner( "" );
                board.setOldOwnerId( 0 );
                board.setBidder( "" );
                board.setBidderId( 0 );
                boardTable.insertAuctionBoard( board );
                house.setOnSale( true ); // 競売中に設定
                house.setPurchaseBasement( true ); // 地下アジト未購入に設定
                dateTime = dateTime.AddDays( Config.HOUSE_TAX_INTERVAL );
                house.setTaxDeadline( dateTime );
                HouseTable.getInstance().updateHouse( house ); // DBに書き込み
                // 以前の所有者のアジトを消す
                foreach ( L1Clan clan in L1World.getInstance().getAllClans() )
                {
                    if ( clan.getHouseId() == houseId )
                    {
                        clan.setHouseId( 0 );
                        ClanTable.getInstance().updateClan( clan );
                    }
                }
            }
        }
    }
}
